package com.lagou.controller;

import cn.hutool.core.util.RandomUtil;
import com.lagou.model.Result;
import com.lagou.service.IAuthCodeService;
import com.lagou.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/code")
@RefreshScope
@Slf4j
public class AuthCodeController {

    @Reference
    private IAuthCodeService authCodeService;

    @Reference
    private IEmailService emailService;



    @GetMapping("/create/{email}")
    public boolean createAuthCode(@PathVariable("email") String email) {
        log.info("  createAuthCode  email:{}.  ",email);
        String code = RandomUtil.randomNumbers(4);
        authCodeService.createNewCode(email, code);
        return emailService.sendEmail(email, code);
    }
    @GetMapping("/validate/{email}/{code}")
    public Result validateAuthCode(@PathVariable("email") String email, @PathVariable("code") String code) {
        return authCodeService.validateAuthCode(email, code);
    }
}
